
<?php 
	/* include "ewcfg50.php" ;
	include "ewmysql50.php" ;
	include "phpfn50.php";
	include "userfn50.php" ; */
?>
<?php
// Open connection to the database
$conn = ew_Connect();
?>
<?php
 class cActivity{
	function setClass($qty){
	if ($qty !='0' ||$qty>0){
	return " Class='filled' ";
		}else{
		return " ";
		}
	}
	function getDetails( $groupid , $ParentId, $logdate)
	{
		global $conn;
		//$query_Recordset2 = "SELECT * FROM tb_lookups WHERE RowNum = ".$groupid." ORDER BY Name ASC ";
		$query_Recordset2  ="SELECT * FROM (
				SELECT 
					l.ID,
					l.RowNum,
					`l`.`LookUp` AS `Lookup`,
					`l`.`Name` AS `Name`,
					ifnull(`a`.`Value`,0) AS `Quantity`,
					`a`.`ParentID` AS `ParentId`,
					a.Description as Description,
					l.SortId as SortId
				FROM `tb_lookups` `l` LEFT JOIN `tb_activity` `a` ON (`a`.`LookupID` = `l`.`ID`)
				 WHERE a.ParentID = ".$ParentId." 
				 AND `l`.`Status`=1
				UNION ALL
				SELECT
					l.ID,
					l.RowNum,
					`l`.`LookUp` AS `Lookup`,
					`l`.`Name` AS `Name`,
					0.00 AS `Quantity`,
					0 AS `ParentId`,
					'-' as Description ,
					SortId as SortId
				FROM  `tb_lookups` `l`  
				 WHERE `l`.`ID` NOT IN (SELECT LookupID FROM tb_activity WHERE ParentID =".$ParentId.")
				 AND `l`.`Status`=1
	) q WHERE q.RowNum=".$groupid." order by q.RowNum, q.Lookup, q.SortId, q.Name ASC";	

		//$query_Recordset2 = " call sp_activity_log(0)";
		$rs = $conn->Execute($query_Recordset2);
		echo "<TABLE class='Details'>";
		do {
		//echo ":-------------->".$rs->fields('Description');
			echo "<TR class='".$rs->fields('Lookup')."'>";
			echo "<TD nowrap >";
			echo $rs->fields('Name').": </TD>";
			echo "<TD>";
			echo "<input type='text' ".$this->setClass($rs->fields('Quantity'))." ".$this->CheckPermission( $logdate )." name='lookup".$rs->fields('ID')."' value=".$rs->fields('Quantity')." style='width:60px' onChange=".chr(34)."update_db('".base64_encode($ParentId)."','".base64_encode($rs->fields('ID'))."',this, '".base64_encode('tb_activity')."', '".base64_encode('Value')."');".chr(34)."  onFocus='setFocusColor(this);' onBlur='resetFocusColor(this);' >";
			echo "</TD>";
			echo "<TD>";
			echo "<input type='text' name='notes".$rs->fields('ID')."'  value='".$rs->fields('Description')."'  onChange=".chr(34)."update_db('".base64_encode($ParentId)."','".base64_encode($rs->fields('ID'))."',this, '".base64_encode('tb_activity')."', '".base64_encode('Description')."');".chr(34).$this->CheckPermission( $logdate )." onFocus='setFocusColor(this);' onBlur='resetFocusColor(this);' >";
			echo "</TD>";
			echo "</TR>";
			$rs->MoveNext();
		} while (!$rs->EOF); 
		
		echo "</TABLE>";
	$rs->Close();
	}
	

	function ActivityLookup($ParentId, $logdate){
		global $conn;
		$query_RecLookUps = "SELECT DISTINCT RowNum FROM tb_lookups ORDER BY RowNum ASC";
		$rs = $conn->Execute($query_RecLookUps);
		echo "<table class='MainLookUp'>";
		do {
			echo "<td width='100%'  valign='top' nowrap>";
			//echo $_SESSION["CHECK_USER_PERMISSION"]."---------->";
			$this->getDetails( $rs->fields('RowNum'),$ParentId, $logdate);
			echo "</td>";
		$rs->MoveNext();
		} while (!$rs->EOF);
		echo "</table>";

		$rs->Close();
	}

	function CheckPermission( $checkDate ){
		$status="";
		$t = getdate();
		$today = sprintf("%04d/%02d/%02d", $t['year'] , $t['mon'] , $t['mday']);
		// $today = "$t[year]/$t[mon]/$t[mday]";
	
	//echo $today;
		if($checkDate=="" || $_SESSION["USER_GROUP"] =='admin' && $_GET["__e"]=='-1'){
			$status = " ";
		}
		else if(ew_FormatDateTime($today,5) <> ew_FormatDateTime($checkDate,5))
		{
			$status = " disabled ";			
		}
		
		return $status;
	}

}

?>
